package org.jeecg.modules.message.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.message.WebSocket.WebSocketServer;
import org.jeecg.modules.message.service.IProcurementMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * WebSocket消息控制层
 * 实现消息发送和接收的两端互联功能
 */
@Api(tags="WebSocket消息通信")
@RestController
@RequestMapping("/ws/message")
@Slf4j
public class WSMessageController {

    @Autowired
    private IProcurementMessageService procurementMessageService;

    /**
     * 向指定用户发送WebSocket消息
     */
    @AutoLog(value = "WebSocket-发送消息给指定用户")
    @ApiOperation(value="WebSocket-发送消息给指定用户", notes="WebSocket-发送消息给指定用户")
    @PostMapping(value = "/sendToUser")
    public Result<?> sendToUser(@RequestParam(name="userId") String userId,
                                @RequestParam(name="username") String username,
                                @RequestParam(name="title") String title,
                                @RequestParam(name="content") String content,
                                @RequestParam(name="businessType", required = false) String businessType,
                                @RequestParam(name="businessId", required = false) String businessId) {
        // 参数校验
        if (StringUtils.isEmpty(userId)) {
            log.warn("【WebSocket】发送消息失败: userId不能为空");
            return Result.error("userId不能为空");
        }
        if (StringUtils.isEmpty(title)) {
            log.warn("【WebSocket】发送消息失败: title不能为空");
            return Result.error("title不能为空");
        }
        if (StringUtils.isEmpty(content)) {
            log.warn("【WebSocket】发送消息失败: content不能为空");
            return Result.error("content不能为空");
        }
        return procurementMessageService.sendToUser(userId,username, title, content, businessType, businessId);
    }

    /**
     * 向多个用户发送WebSocket消息
     */
    @AutoLog(value = "WebSocket-发送消息给多个用户")
    @ApiOperation(value="WebSocket-发送消息给多个用户", notes="WebSocket-发送消息给多个用户")
    @PostMapping(value = "/sendToUsers")
    public Result<?> sendToUsers(@RequestParam(name="userIds") String userIds,
                                 @RequestParam(name="title") String title,
                                 @RequestParam(name="content") String content) {
        // 参数校验
        if (StringUtils.isEmpty(userIds)) {
            log.warn("【WebSocket】发送消息失败: userIds不能为空");
            return Result.error("userIds不能为空");
        }
        if (StringUtils.isEmpty(title)) {
            log.warn("【WebSocket】发送消息失败: title不能为空");
            return Result.error("title不能为空");
        }
        if (StringUtils.isEmpty(content)) {
            log.warn("【WebSocket】发送消息失败: content不能为空");
            return Result.error("content不能为空");
        }
        return procurementMessageService.sendToUsers(userIds, title, content);
    }

    /**
     * 广播消息给所有在线用户
     */
    @AutoLog(value = "WebSocket-广播消息")
    @ApiOperation(value="WebSocket-广播消息", notes="WebSocket-广播消息给所有在线用户")
    @PostMapping(value = "/broadcast")
    public Result<?> broadcast(@RequestParam(name="title") String title,
                               @RequestParam(name="content") String content) {
        // 参数校验
        if (StringUtils.isEmpty(title)) {
            log.warn("【WebSocket】广播失败: title不能为空");
            return Result.error("title不能为空");
        }
        if (StringUtils.isEmpty(content)) {
            log.warn("【WebSocket】广播失败: content不能为空");
            return Result.error("content不能为空");
        }
        return procurementMessageService.broadcast(title, content);
    }

    /**
     * 获取在线用户数量
     */
    @AutoLog(value = "WebSocket-获取在线用户数量")
    @ApiOperation(value="WebSocket-获取在线用户数量", notes="WebSocket-获取在线用户数量")
    @GetMapping(value = "/onlineCount")
    public Result<?> getOnlineCount() {
        try {
            int count = WebSocketServer.getOnlineCount();
            log.info("【WebSocket状态】当前在线用户数量: {}", count);
            return Result.OK(count);
        } catch (Exception e) {
            log.error("【WebSocket状态】获取在线用户数量失败", e);
            return Result.error("获取在线用户数量失败: " + e.getMessage());
        }
    }

    /**
     * 发送并推送消息（保存到数据库并通过WebSocket推送）
     */
    @AutoLog(value = "WebSocket-发送并推送消息")
    @ApiOperation(value="WebSocket-发送并推送消息", notes="WebSocket-发送并推送消息（保存到数据库并通过WebSocket推送）")
    @PostMapping(value = "/sendAndPush")
    public Result<?> sendAndPush(@RequestParam(name="title") String title,
                                 @RequestParam(name="content") String content,
                                 @RequestParam(name="receiver") String receiver,
                                 @RequestParam(name="businessType", required = false) String businessType,
                                 @RequestParam(name="businessId", required = false) String businessId) {
        // 参数校验
        if (StringUtils.isEmpty(title)) {
            log.warn("【WebSocket】发送并推送失败: title不能为空");
            return Result.error("title不能为空");
        }
        if (StringUtils.isEmpty(content)) {
            log.warn("【WebSocket】发送并推送失败: content不能为空");
            return Result.error("content不能为空");
        }
        if (StringUtils.isEmpty(receiver)) {
            log.warn("【WebSocket】发送并推送失败: receiver不能为空");
            return Result.error("receiver不能为空");
        }
        return procurementMessageService.sendAndPush(title, content, receiver, businessType, businessId);
    }
}
